package no.knowit.selectModel;

import java.util.ArrayList;
import java.util.List;

import no.knowit.entities.Ressurs;

import org.apache.tapestry5.OptionGroupModel;
import org.apache.tapestry5.OptionModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.internal.OptionModelImpl;
import org.apache.tapestry5.util.AbstractSelectModel;

public class RessursSelectModel extends AbstractSelectModel implements
		ValueEncoder<Object> {
	private List<Ressurs> ressurser;

	public RessursSelectModel(List<Ressurs> ressurser) {
		this.ressurser = ressurser;
	}

	@Log
	public String toClient(Object innkommendeRessurs) {
		Ressurs ressurs = (Ressurs) innkommendeRessurs;
		return ressurs.getId().toString();
	}

	public Object toValue(String input) {
		Long id = Long.parseLong(input);
		for (Ressurs hentetRessurs : ressurser) {
			if (id.equals(hentetRessurs.getId())) {
				return hentetRessurs;
			}
		}
		return null;
	}

	public List<OptionGroupModel> getOptionGroups() {
		return null;
	}

	public List<OptionModel> getOptions() {
		List<OptionModel> options = new ArrayList<OptionModel>();
		for (Ressurs hentetRessurs : ressurser) {
			options.add(new OptionModelImpl(hentetRessurs.getFornavn() + " "
					+ hentetRessurs.getEtternavn(), hentetRessurs));
		}
		return options;
	}

	public List<Ressurs> getRessurser() {
		return ressurser;
	}

	public void setRessurser(List<Ressurs> ressurser) {
		this.ressurser = ressurser;
	}

}
